/****************************************************************************

An ADC abstraction layer.

Author:			Jon Maloney
Last Edited:	27/05/2012

****************************************************************************/
#include "types.h"
#include "mqueue.h"
#include "p24FJ64GA004.h"
#include "microRTOS.h"

/****************************************************************************
				Defines
****************************************************************************/

#define _CHAN 			u.s.ADCChan
#define _CURR_HARMONIC	u.s.HarmonicNb
#define _NB_HARMONICS	u.s.NbHarmonics

/****************************************************************************
				Structs Enums and Unions
****************************************************************************/

/****************************************************************************
A special data type that represents the sample and the sample ID. 
	The entire structure is 16 bits.
	The first 12 bits are used for storing the value read from the ADC
	The last 4 bits are used to identify the source of the sample 
	i.e. the sample ID.
****************************************************************************/
typedef union
{
	TUINT16 sampleAndSampleID;
	struct
	{
		unsigned sampleID	:4;	//Channel sample came from.
								//		1111 = chanel 15
								//		1110 = chanel 14
								//		....
								//		0001 = chanel 1
								//		0000 = chanel 0
		unsigned value	:12;
	}s;
}SampleType;

typedef struct
{
	union
	{
		UINT32 SampleHandle;
		struct
		{
			unsigned ADCChan		:4;
			unsigned NbHarmonics	:14;
			unsigned HarmonicNb		:14;
		}s;
	}u;
	m_QueueHandle xADCQueue;
	TCB * sampleThread;
}T_SAMPLE_HANDLE;

/****************************************************************************
A function to initialise the ADC.
****************************************************************************/
void ADC_Init(void);

void setupDefaultADCConfiguration(void);

void ADC_process_Sample( void * pvParameters );

void ADC_StartSampling(void);

void ADCSampleHandler( void * Start_Of_ADC_Sample_List);
